package com.taobao.askwho.action;
import java.net.MalformedURLException;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.apache.solr.client.solrj.SolrServerException;
import org.apache.solr.client.solrj.response.QueryResponse;
import org.apache.solr.common.SolrDocument;
import org.apache.solr.common.SolrDocumentList;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.taobao.askwho.ao.ComplexAo;
import com.taobao.askwho.po.UserInfo;
import com.taobao.askwho.utils.SolrUtil;
import com.taobao.askwho.utils.WorldPressCloudTagUtils;
import com.taobao.askwho.utils.session.SessionUtil;
import com.taobao.askwho.vo.PersonalPageRecordVo;

@Controller                 
@RequestMapping("/urljump.do")
public class UrlJumpAction {
	
	Logger logger = Logger.getLogger(UrlJumpAction.class);
	//首页
	@RequestMapping(params = "method=gotoHomePage")
    public ModelAndView gotoHomePage() {
		ModelAndView view = new ModelAndView("/jsp/home");
		//List<Tag> cloudTagList = CloudTagUtils.get().getCloudTags();
		List<WorldPressCloudTagUtils.CloudTagPo> cloudTagList = WorldPressCloudTagUtils.get().getCloudTags();
		view.addObject("cloudTags", cloudTagList);
		return view;
    }
	//个人主页
	@RequestMapping(params = "method=gotoPersonPage")
	public ModelAndView gotoPersonPage(HttpServletRequest request) {
		UserInfo userInfo = SessionUtil.getUserInfo(request);
		if(userInfo == null) {
			ModelAndView view = new ModelAndView("/jsp/user/errorpage");
			view.addObject("msg", "请先登录");
			return view;
		}
		
		ModelAndView view = new ModelAndView("/jsp/user/personalpage");
		List<PersonalPageRecordVo> itemList = ComplexAo.get().getPersonalInfoAll(userInfo.getUuid());
		view.addObject("userUuid", userInfo.getUuid());
		view.addObject("itemList",itemList);
		return view;
	}
	//分享某个用户的共享给别人（公开部分）
	@RequestMapping(params = "method=shareAllPublicTags")
	public ModelAndView shareAllPublicTags(String otherUuid, HttpServletRequest request) {
		UserInfo info = SessionUtil.getUserInfo(request);
		String myUuid = info.getUuid();
		
		List<PersonalPageRecordVo> itemList = ComplexAo.get().getPersonalInfoAll(otherUuid);
		Iterator<PersonalPageRecordVo> iter = itemList.iterator();
		while(iter.hasNext()) {
			PersonalPageRecordVo vo = iter.next();
			if(!vo.isPublic())
				iter.remove();
		}
		ModelAndView view = new ModelAndView("/jsp/user/mypublic");
		view.addObject("otherUuid", otherUuid);
		view.addObject("myUuid", myUuid);
		view.addObject("itemList",itemList);
		view.addObject("originusername",info.getUserName());
		return view;
	}
	
	@RequestMapping(params = "method=gotoLoginPage")
	public ModelAndView login(HttpServletRequest request, String errorMsg){
		ModelAndView view = new ModelAndView("/jsp/index");
		view.addObject("errorMsg", errorMsg);
		return view;
	}
	
	@RequestMapping(params = "method=gotoLogoutPage")
	public String logout(HttpServletRequest request){
		UserInfo user = SessionUtil.getUserInfo(request);
		if(user != null) {
			SessionUtil.setUserInfo(request, null);
		}
		return "redirect:/urljump.do?method=gotoLoginPage";
	}	
	
	//查询
	@RequestMapping(params = "method=searchMain")
	public ModelAndView searchMain(String keyword) {
		ModelAndView view = new ModelAndView("/jsp/search/searchresult");
        List<PersonalPageRecordVo> tagRelationList = new ArrayList<PersonalPageRecordVo>();
        try {
        	QueryResponse rsp = SolrUtil.get().querySingleColumn(SolrUtil.SOLOR_INDEX, keyword, SolrUtil.QUERY_LARGE_NUMBER);
        	SolrDocumentList list = rsp.getResults();
    		for(SolrDocument doc : list) {
    			try {
    				PersonalPageRecordVo rel = new PersonalPageRecordVo();
					rel.setUuid(doc.getFieldValue("uuid").toString());
					Set<String> set = new HashSet<String>();	//FIXME 全部功能完成后，去掉
					set.add(doc.getFieldValue("tagname").toString());
					rel.setTagNamesSet(set);
					String type = doc.getFieldValue("type").toString(); 
					rel.setType(type);
					rel.setUserUuid(doc.getFieldValue("user_uuid").toString());
					rel.setPublic(true);//能搜出来的都是公开
					if(type.equals("person")) 
						rel.getPersonInfo().setUuid(doc.getFieldValue("related_uuid").toString());
					else if(type.equals("url"))
						rel.getUrlInfo().setUuid(doc.getFieldValue("related_uuid").toString());
					else 
						continue;
					tagRelationList.add(rel);
				} catch (Exception e) {
					logger.error("", e);
				}
    		}
		} catch (MalformedURLException e) {
			logger.error("", e);
		} catch (SolrServerException e) {
			logger.error("", e);
		}
        		
        List<PersonalPageRecordVo> itemList = ComplexAo.get().findResultByVoList(tagRelationList);
		view.addObject("keyword", keyword);
		view.addObject("itemList", itemList);
		return view;
	}
	//关于问谁
	@RequestMapping(params = "method=gotoAboutusPage")
	public ModelAndView gotoSearchUrlPage() {
		ModelAndView view = new ModelAndView("/jsp/aboutus");
		return view;
	}	
//	//查询Url界面
//	@RequestMapping(params = "method=gotoSearchUrlPage")
//	public ModelAndView gotoSearchUrlPage() {
//		ModelAndView view = new ModelAndView("/jsp/user/personalpage");
//		return view;
//	}
//	//查询人界面
//	@RequestMapping(params = "method=gotoSearchPersonPage")
//	public ModelAndView gotoSearchPersonPage() {
//		ModelAndView view = new ModelAndView("/jsp/user/personalpage");
//		return view;
//	}
//	//查询群界面
//	@RequestMapping(params = "method=gotoSearchGroupPage")
//	public ModelAndView gotoSearchGroupPage() {
//		ModelAndView view = new ModelAndView("/jsp/user/personalpage");
//		return view;
//	}	
//	//某一个Tag的详细，同一个Tag的所有结果
//	@RequestMapping(params = "method=gotoTagDetail")
//	public ModelAndView gotoTagDetail() {
//		ModelAndView view = new ModelAndView("/jsp/user/personalpage");
//		return view;
//	}
//	//某一结果的所有Tag列表，即查看全部
//	@RequestMapping(params = "method=gotoAllTagsForResult")
//	public ModelAndView gotoAllTagsForResult() {
//		ModelAndView view = new ModelAndView("/jsp/user/personalpage");
//		return view;
//	}	
}